﻿using NAnt.Core.Attributes;
using NAnt.SVN.Resources;
using System;
using NAnt.Core;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Implementation of the commit command
    /// You can run this tasc with the svncopy tag in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("svncommit")]
    public class Commit : SVNTask
    {
        #region Private Fields

        string path = string.Empty;
        string message = string.Empty;
        bool nounlock = false;
        string file = string.Empty;


        #endregion

        #region Public Fields

        /// <summary>
        /// Path attribute
        /// </summary>
        [TaskAttribute("path", Required = false)]
        [StringValidator()]
        public string Path
        {
            get
            {
                return path;
            }
            set
            {
                path = value;
            }
        }

        /// <summary>
        /// File attribute
        /// </summary>
        [TaskAttribute("file", Required = false)]
        [StringValidator()]
        public string File
        {
            get
            {
                return file;
            }
            set
            {
                file = value;
            }
        }

        /// <summary>
        /// The message
        /// </summary>
        [TaskAttribute("message", Required = true)]
        [StringValidator()]
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        /// <summary>
        /// True if a path attribute is given
        /// </summary>
        public bool HasPath
        {
            get 
            {
                return !string.IsNullOrEmpty(Path);
            }
        }

        /// <summary>
        /// True if a file attribute is given
        /// </summary>
        public bool HasFile
        {
            get
            {
                return !string.IsNullOrEmpty(File);
            }
        }

        #endregion

        /// <summary>
        /// Initialize command
        /// </summary>
        protected override void InitializeSVNTask()
        {
            Command = SVNCommands.COMMIT;
            if (HasFile)
            {
                Options.Add(SVNFlags.FILE);
                Options.Add(quote(File));
            }
            else
            {
                if (HasPath)
                {
                    Options.Add(Path);
                }
                else 
                {
                    throw new BuildException(Util.Strings.GetString("SVN0316"));
                }
            }
            Options.Add(SVNFlags.MESSAGE);
            Options.Add(quote(Message));
        }
    }
}
